/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.print.attribute.standard.PageRanges;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.ICollecDetailDao;
import com.ce.pms.charge.model.CollecDetail;
import com.ce.pms.charge.vo.CollectionDetailVO;
import com.ce.pms.common.annotation.AssociatedBuildingQuery;
import com.ce.pms.common.annotation.AssociatedPrecinctQuery;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：<br>
 * @author 罗军林 创建日期：2008-10-7
 */
public class CollecDetailDaoImpl extends PmsBaseDaoImpl<CollecDetail> implements
		ICollecDetailDao {

	/**
	 * 功能描述:校验文件中的ID，房号， 姓名，帐户号码，金额是否与数据库中的当前用户权限下对应ID，房号，姓名，帐户号码，金额是否匹配<br>
	 * @param bankOutInBean
	 * @return 校验通过返回true<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	@AssociatedPrecinctQuery(alias = "detail", beginStr = " AND ", endStr = " ", insertAfterPoint = "WHERE 1=1",property4Precinct="collecFile.period.chargePrecinct.keyword")
	public boolean checkExcelDataIsSameDB(CollectionDetailVO bankOutInBean){
		Map<String, Object> parameter=new HashMap<String, Object>();
		StringBuffer hql=new StringBuffer();
		hql.append("SELECT Count(detail) FROM CollecDetail detail WHERE 1=1" );
		hql.append(buildCondition(bankOutInBean,parameter));

		Query query=createNameQuery(hql.toString(), parameter);
		Long result=(Long)query.uniqueResult();
		return result==1;
	}

	/**
	 * 功能描述:查询托收明细<br>
	 * @param searchVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-14<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public Page queryCollectionDetail(CollectionDetailVO searchVO){
		Map<String, Object> parameter=new HashMap<String, Object>();
		StringBuffer hql=new StringBuffer();
		hql.append("FROM CollecDetail detail WHERE 1=1 ");
		hql.append(buildCondition(searchVO, parameter));
		return this.pagedNameQueryByPageNo(hql.toString(), searchVO.getPageNo(), searchVO.getPageSize(), parameter);
	}

	/**
	 * 功能描述:查询所有指定主键的托收明细<br>
	 * @param collecDetailIds
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-8<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<CollecDetail> queryCollecDetailByCollectionId(Long collectionId){
		StringBuffer hql=new StringBuffer();
		hql.append("FROM CollecDetail detail WHERE detail.collecFile.keyword =?");
		Query query=createQuery(hql.toString(), collectionId);
		return query.list();
	}

	/**
	 * 功能描述:查询所有指定主键的托收明细<br>
	 * @param collecDetailIds
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-8<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<CollecDetail> queryCollecDetailByIDS(Set<Long> collecDetailIds){
		StringBuffer hql=new StringBuffer();
		hql.append("FROM CollecDetail detail WHERE detail.collecDetailId in (");
		for(int i=0;i<collecDetailIds.size();i++){
			if(i==collecDetailIds.size()-1){
				hql.append("?");
			}else{
				hql.append("?,");
			}
		}
		hql.append(")");

		Query query=createQuery(hql.toString(), collecDetailIds.toArray());
		return query.list();
	}

	/**
	 * 功能描述:根据回盘文件数据更新划款状态<br>
	 * @param bankOutInMap<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-8<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void updateCollecDetailStatus(Map<Long,CollectionDetailVO> bankOutInMap){
		List<Object[]> parameters=new ArrayList<Object[]>();
		String sql="UPDATE FEE_COLLECTION_DETAIL SET STATUZ=? WHERE COLLECTION_DETAIL_ID=?";
		for(CollectionDetailVO bankOutInBean : bankOutInMap.values()){
			parameters.add(new Object[]{bankOutInBean.getStatus(),bankOutInBean.getCollectionDetailId()});
		}
		executeSQLBatch(sql, parameters);
	}

	/**
	 * 功能描述:构建查询条件<br>
	 * @param searchVO
	 * @param parameter
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-14<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildCondition(CollectionDetailVO searchVO,Map<String, Object> parameter){
		StringBuffer hql=new StringBuffer();
		if(null!=searchVO && null!=searchVO.getCollectionDetailId()){
			hql.append(" and detail.collecDetailId=:collecDetailId ");
			parameter.put("collecDetailId", searchVO.getCollectionDetailId());
		}
		if(null!=searchVO && null!=searchVO.getHouseNum()){
			hql.append(" and detail.house.houseNumber like :houseNumber ");
			parameter.put("houseNumber", "%" + searchVO.getHouseNum() + "%");
		}
		if(null!=searchVO && null!=searchVO.getAccOwner()){
			hql.append(" and detail.accOwner like :accOwner ");
			parameter.put("accOwner", "%" +  searchVO.getAccOwner() +  "%");
		}
		if(null!=searchVO && null!=searchVO.getAccNumber()){
			hql.append(" and detail.accNumber like :accNumber ");
			parameter.put("accNumber", "%" +  searchVO.getAccNumber() +  "%");
		}
		if(null!=searchVO && null!=searchVO.getAmt()){
			hql.append(" and detail.amt=:amt ");
			parameter.put("amt", searchVO.getAmt());
		}
		if(null!=searchVO && null!=searchVO.getCollectionId()){
			hql.append(" and detail.collecFile.keyword=:collectionId ");
			parameter.put("collectionId", searchVO.getCollectionId());
		}
		return hql.toString();
	}

}
